t=(1:400)/100
y1 = dchisq(t,df=1)
plot(t, y1, type="l",lwd=2,col="black",ylim=c(0,1.5),ylab="density")
## now, adding new plots
y2 = dchisq(t,df=2)
y3 = dchisq(t,df=3)
y4 = dchisq(t,df=4)
lines(t, y2, lwd=2,col="red")
lines(t, y3, lwd=2,col="blue")
lines(t, y4, lwd=2,col="green")
## and we add a legend, too
legend(x=3,y=1,legend=c("df=1", "df=2", "df=3", "df=4"),
col=c("black", "red", "blue", "green"), lty="solid", cex=0.8)
Note that x=3, y=1 represent the position of the upper left point of the legend. We also used lty="solid" for solid line type (which is default). Instead of the word "solid", we could use lty=2. You can google which numbers represent which line types. Also, cex for the expansion (i.e. size) of the object. Play with different values to see how it affects the plot.
To add a straight line to a plot, you can use lines(). However, this requires creating vectors x and y. Instead, you one can use abline() for linear function \(y=ax+b\). The function abline() has input parameters a and b (the slope and the intercept). The abline() function can be used for vertical and horizontal lines, as well (with parameters v and h, respectively)
abline(a=0.5, b=2, lwd=4, lty='dashed')
